home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / btsys.arc / BTCREAT.C < prev    next >
Text File  |  1984-12-14  |  2KB  |  78 lines

  1. /*    btcreat    */
  2. #include <stdio.h>
  3. #include <btextern.h>
  4.  
  5. int btcreat (filnam)
  6. char filnam[];
  7.  
  8. /*    create new instance of an index    */
  9. {
  10.     int i, occurs, ptrarea, keyarea, *ip, left;
  11.     char *cp;
  12.  
  13.     if ((fp = fopen(filnam, "wrb")) == NULL)
  14.     BTSETCOD (NULL, NULL, 4);    /* file open error */
  15.     if ((openkt = btopnkt ()) == -1)
  16.     BTSETCOD (openkt, NULL, 63);    /* no control block */
  17.     if (!(btfilar[openkt].infobuf = 
  18.     (int *)calloc (LBLEN, sizeof (char))))
  19.     BTSETCOD (openkt, NULL, 7);    /* no memory */
  20.     if (!(btfilar[openkt].filbuf = 
  21.     (int *)calloc (LBLEN, sizeof (char))))
  22.     BTSETCOD (openkt, NULL, 7);    /* no memory */
  23.     strcpy (btfilar[openkt].curkey,"                ");
  24.     btfilar[openkt].keylen = gkeylen;
  25.     strcpy (btfilar[openkt].filnam, gfilnam);
  26.     btfilar[openkt].handle = openkt;
  27.     btfilar[openkt].root = 1;
  28.     btfilar[openkt].fdesc = fp;
  29.  
  30. /*    initialize info index record    */
  31.     ip = btfilar[openkt].infobuf;
  32.     *ip++ = 1;        /*value of root record */
  33.     *ip++ = gkeylen;    /*set key length */
  34.     *ip = 1;        /*rec # 1 unavailable */
  35.  
  36. /*    now initialize rest with zeros    */
  37.     left = 1 + (LBLEN / 2 - 3);    /* left to be initialized */
  38.     while (--left)
  39.     *++ip = 0;        /* rest are available */
  40.     movmem ((char *)btfilar[openkt].infobuf,
  41.     (char *)btfilar[openkt].filbuf, LBLEN);
  42. /*    write index info record from buffer */
  43.     if (( i = btwrit (openkt,0)))
  44.     BTSETCOD (openkt,0,i);    /* set write error */
  45.  
  46. /*    now initialize root record */
  47.     occurs = (LBLEN - 1) / (gkeylen + 3);
  48.     btfilar[openkt].ikeyptr = 
  49.     (char *)(btfilar[openkt].filbuf + occurs);
  50.  
  51. /*    initialize pointer area with NULL */
  52.     ip = btfilar[openkt].filbuf;
  53.     ip3 = ip + occurs;
  54.     while (ip < ip3)
  55.      *ip++ = NULL;
  56.  
  57. /*    intialize keyarea with null strings    */
  58.     cp = btfilar[openkt].ikeyptr;    /* initialize pointer */
  59.     cp3 = (char *) btfilar[openkt].filbuf + LBLEN;
  60.     while (cp < cp3)
  61.     *cp++ = '\0';
  62.  
  63. /*    initialize flag to pointer to data */
  64.     cp = (char *) btfilar[openkt].filbuf;
  65.     cp += (LBLEN - 1);
  66.     *cp = 'Y';
  67.  
  68. /*    write root record    */
  69.     if (( i = btwrit (openkt, 1)))
  70.     BTSETCOD (openkt, 1, i);    /* set write error */
  71.  
  72. /*    return value to outside world */
  73.     gfilhand = openkt;
  74.     gretcode = 0;
  75.     return (0);
  76. }
  77. /* end of btcreat */
  78.